Ext.define('PJ.view.trainingprogram.PNEdit', {
	extend: 'AM.default.DefaultWindowForm',
	alias: ['widget.pNEdit'],
	requires: ['AM.default.jsonp.DefaultSubmitJsonp',
	           'AM.default.DefaultDateField'],

	modal: true,// 开启不允许点击其他组件
	frame: true,
	notEdit: true,
	width: 350,
	height:105,
	layout: 'form',
	isSame: false,
	autoHeight: true,

	initComponent: function(){
    	var me = this;
    	var bt = '<span style="color:red;">*</span>';
    	me.items= [{
    		border: false,
    		xtype: "container",
    		width: 320,
    		layout: 'column',
    		items: [{
    			xtype: 'textfield',
    			fieldLabel: bt + '请填写人数',
    			labelAlign: 'right',
    			labelWidth: 80,
    			name: 'peopleNum',
    			regex: /^[1-9]\d*|0$/,
    			regexText: '人数只能输入数字！',
    			allowBlank: false,
    			columnWidth: 1,
    			value: me.thisValue
    		}]
    	}];
    	this.buttons = [{
            text: '确定',
            hidden: me.isView,
            handler: function(button, event){
                var win = button.up('window');
                var form = win.down("form");
                var baseForm = form.getForm();
                var peopleNum = baseForm.findField("peopleNum").getValue();
                var participantsNumList = win.grid;
                participantsNumList.getSelectionModel().deselectAll();
                participantsNumList.getSelectionModel().select(Number(win.myRowIndex));
   				var rec = participantsNumList.getSelectionModel().getSelection()[0];
   				var win_ = participantsNumList.up("window");
   				var oldParticipantsNumList = win_.down("oldParticipantsNumList");
   				if(win.flag == 1){
   					rec.set("contrastCityValue",peopleNum);
   	   				var contrastCountyValue = rec.get("contrastCountyValue");
   	   				rec.set("contrastCountValue",Number(peopleNum) + Number(contrastCountyValue));
   	   				if(oldParticipantsNumList){
   	   					oldParticipantsNumList.getSelectionModel().deselectAll();
   	       				oldParticipantsNumList.getSelectionModel().select(Number(me.myRowIndex));
   	       				var rec_ = oldParticipantsNumList.getSelectionModel().getSelection()[0];
   	       				oldParticipantsNumList.getSelectionModel().deselectAll();
   	       				if(peopleNum != rec_.get("contrastCityValue")){
   	       					$(Ext.get("contrastCityValue_" + me.myID).dom).find("div")[0].style.color = "red";
   	       				}else{
   	       					$(Ext.get("contrastCityValue_" + me.myID).dom).find("div")[0].style.color = "#000000";
   	       				}
   	       				var oldcontrastCountyValue = rec_.get("contrastCountyValue");
	       				var contrastCountyValue = rec.get("contrastCountyValue");
	       				if(contrastCountyValue != oldcontrastCountyValue){
	       					$(Ext.get("contrastCountyValue_" + me.myID).dom).find("div")[0].style.color = "red";
	       				}else{
	       					$(Ext.get("contrastCountyValue_" + me.myID).dom).find("div")[0].style.color = "#000000";
	       				}
   	       				var oldContrastCountValue = rec_.get("contrastCountValue");
   	       				var contrastCountValue = rec.get("contrastCountValue");
   	       				if(contrastCountValue != oldContrastCountValue){
   	       					$(Ext.get("contrastCountValue_" + me.myID).dom).find("div")[0].style.color = "red";
   	       				}else{
   	       					$(Ext.get("contrastCountValue_" + me.myID).dom).find("div")[0].style.color = "#000000";
   	       				}
	   	       			participantsNumList.getSelectionModel().selectAll();
		   				var recs = participantsNumList.getSelectionModel().getSelection();
		   				participantsNumList.getSelectionModel().deselectAll();
		   				var recTotal = null;
		   				var cityTotal = 0;
		   				for(i in recs){
		   					if(recs[i].get("contrastObject") == "合计"){
		   						recTotal = recs[i];
		   					}else{
		   						cityTotal += Number(recs[i].get("contrastCityValue"));
		   					}
		   				}
		   				if(recTotal){
		   					recTotal.set("contrastCityValue",cityTotal);
		   				}
		   				oldParticipantsNumList.getSelectionModel().select(0);
		       			var rect = oldParticipantsNumList.getSelectionModel().getSelection()[0];
		       			oldParticipantsNumList.getSelectionModel().deselectAll();
		       			participantsNumList.getSelectionModel().select(0);
		   				var recnt = participantsNumList.getSelectionModel().getSelection();
		   				participantsNumList.getSelectionModel().deselectAll();
		   				recnt[0].set("contrastCountValue",Number(cityTotal + Number(recnt[0].get("contrastCountyValue"))));
		       			if(rect.get("contrastCityValue") != cityTotal){
		       				$(Ext.get("contrastCityValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "red";
		       			}else{
		       				$(Ext.get("contrastCityValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "#000000";
		       			}
		       			if(rect.get("contrastCountyValue") != recnt[0].get("contrastCountyValue")){
		       				$(Ext.get("contrastCountyValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "red";
		       			}else{
		       				$(Ext.get("contrastCountyValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "#000000";
		       			}
		       			if(rect.get("contrastCountValue") != Number(cityTotal + Number(recnt[0].get("contrastCountyValue")))){
		       				$(Ext.get("contrastCountValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "red";
		       			}else{
		       				$(Ext.get("contrastCountValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "#000000";
		       			}
   	   				}else{
   	   					if(me.firstValue != peopleNum){
   	   						$(Ext.get("contrastCityValue_" + me.myID).dom).find("div")[0].style.color = "red";
   	   					}else{
   	   						$(Ext.get("contrastCityValue_" + me.myID).dom).find("div")[0].style.color = "#000000";
   	   					}
   	   					if(me.firstTotal != (Number(peopleNum) + Number(me.contrastCountyValue))){
   	   						$(Ext.get("contrastCountValue_" + me.myID).dom).find("div")[0].style.color = "red";
   	   					}else{
   	   						$(Ext.get("contrastCountValue_" + me.myID).dom).find("div")[0].style.color = "#000000";
   	   					}
   	   					if(me.firstValueOther != me.contrastCountyValue){
   	   						$(Ext.get("contrastCountyValue_" + me.myID).dom).find("div")[0].style.color = "red";
   	   					}else{
   	   						$(Ext.get("contrastCountyValue_" + me.myID).dom).find("div")[0].style.color = "#000000";
   	   					}
	   	   				participantsNumList.getSelectionModel().selectAll();
		   				var recs = participantsNumList.getSelectionModel().getSelection();
		   				participantsNumList.getSelectionModel().deselectAll();
		   				participantsNumList.getSelectionModel().select(0);
		   				var recnt = participantsNumList.getSelectionModel().getSelection();
		   				participantsNumList.getSelectionModel().deselectAll();
		   				var recTotal = null;
		   				var cityTotal = 0;
		   				for(i in recs){
		   					if(recs[i].get("contrastObject") == "合计"){
		   						recTotal = recs[i];
		   					}else{
		   						cityTotal += Number(recs[i].get("contrastCityValue"));
		   					}
		   				}
		   				if(recTotal){
		   					var contrastCountyValueTotal = recTotal.get("contrastCountyValue");
		   					recTotal.set("contrastCityValue",cityTotal);
		   					recTotal.set("contrastCountValue",Number(cityTotal + Number(contrastCountyValueTotal)));
		   				}
   	   					if(me.totalNumber != cityTotal){
   	   						$(Ext.get("contrastCityValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "red";
   	   					}else{
   	   						$(Ext.get("contrastCityValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "#000000";
   	   					}
   	   					if(me.contrastCountyValueTotal != Number(cityTotal + Number(recTotal.get("contrastCountyValue")))){
   	   						$(Ext.get("contrastCountValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "red";
   	   					}else{
   	   						$(Ext.get("contrastCountValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "#000000";
   	   					}
   	   					if(me.contrastCountyValueTotal != recTotal.get("contrastCountyValue")){
	   						$(Ext.get("contrastCountyValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "red";
	   					}else{
	   						$(Ext.get("contrastCountyValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "#000000";
	   					}
   	   				}
	   	   			
   				}else if(win.flag == 2){
       				rec.set("contrastCountyValue",peopleNum);
       				var contrastCityValue = rec.get("contrastCityValue");
       				rec.set("contrastCountValue",Number(peopleNum) + Number(contrastCityValue));
       				if(oldParticipantsNumList){
       					oldParticipantsNumList.getSelectionModel().deselectAll();
	       				oldParticipantsNumList.getSelectionModel().select(Number(me.myRowIndex));
	       				var rec_ = oldParticipantsNumList.getSelectionModel().getSelection()[0];
	       				oldParticipantsNumList.getSelectionModel().deselectAll();
	       				if(peopleNum != rec_.get("contrastCountyValue")){
	       					$(Ext.get("contrastCountyValue_" + me.myID).dom).find("div")[0].style.color = "red";
	       				}else{
	       					$(Ext.get("contrastCountyValue_" + me.myID).dom).find("div")[0].style.color = "#000000";
	       				}
	       				var oldcontrastCityValue = rec_.get("contrastCityValue");
	       				var contrastCityValue = rec.get("contrastCityValue");
	       				if(contrastCityValue != oldcontrastCityValue){
	       					$(Ext.get("contrastCityValue_" + me.myID).dom).find("div")[0].style.color = "red";
	       				}else{
	       					$(Ext.get("contrastCityValue_" + me.myID).dom).find("div")[0].style.color = "#000000";
	       				}
	       				var oldContrastCountValue = rec_.get("contrastCountValue");
	       				var contrastCountValue = rec.get("contrastCountValue");
	       				if(contrastCountValue != oldContrastCountValue){
	       					$(Ext.get("contrastCountValue_" + me.myID).dom).find("div")[0].style.color = "red";
	       				}else{
	       					$(Ext.get("contrastCountValue_" + me.myID).dom).find("div")[0].style.color = "#000000";
	       				}
	       				
	       				participantsNumList.getSelectionModel().selectAll();
		   				var recs = participantsNumList.getSelectionModel().getSelection();
		   				participantsNumList.getSelectionModel().deselectAll();
		   				var recTotal = null;
		   				var cityTotal = 0;
		   				for(i in recs){
		   					if(recs[i].get("contrastObject") == "合计"){
		   						recTotal = recs[i];
		   					}else{
		   						cityTotal += Number(recs[i].get("contrastCountyValue"));
		   					}
		   				}
		   				if(recTotal){
		   					recTotal.set("contrastCountyValue",cityTotal);
		   				}
		   				oldParticipantsNumList.getSelectionModel().select(0);
		       			var rect = oldParticipantsNumList.getSelectionModel().getSelection()[0];
		       			oldParticipantsNumList.getSelectionModel().deselectAll();
		       			participantsNumList.getSelectionModel().select(0);
		   				var recnt = participantsNumList.getSelectionModel().getSelection();
		   				participantsNumList.getSelectionModel().deselectAll();
		   				recnt[0].set("contrastCountValue",Number(cityTotal + Number(recnt[0].get("contrastCityValue"))));
		       			if(rect.get("contrastCountyValue") != cityTotal){
		       				$(Ext.get("contrastCountyValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "red";
		       			}else{
		       				$(Ext.get("contrastCountyValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "#000000";
		       			}
		       			if(rect.get("contrastCountValue") != Number(cityTotal + Number(recnt[0].get("contrastCityValue")))){
		       				$(Ext.get("contrastCountValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "red";
		       			}else{
		       				$(Ext.get("contrastCountValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "#000000";
		       			}
       				}else{
       					if(me.firstValue != peopleNum){
       						$(Ext.get("contrastCountyValue_" + me.myID).dom).find("div")[0].style.color = "red";
   	   					}else{
   	   						$(Ext.get("contrastCountyValue_" + me.myID).dom).find("div")[0].style.color = "#000000";
   	   					}
   	   					if(me.firstTotal != (Number(peopleNum) + Number(me.contrastCityValue))){
   	   						$(Ext.get("contrastCountValue_" + me.myID).dom).find("div")[0].style.color = "red";
   	   					}else{
   	   						$(Ext.get("contrastCountValue_" + me.myID).dom).find("div")[0].style.color = "#000000";
   	   					}
   	   					if(me.firstValueOther != me.contrastCityValue){
	   						$(Ext.get("contrastCityValue_" + me.myID).dom).find("div")[0].style.color = "red";
	   					}else{
	   						$(Ext.get("contrastCityValue_" + me.myID).dom).find("div")[0].style.color = "#000000";
	   					}
	   	   				participantsNumList.getSelectionModel().selectAll();
		   				var recs = participantsNumList.getSelectionModel().getSelection();
		   				participantsNumList.getSelectionModel().deselectAll();
		   				participantsNumList.getSelectionModel().select(0);
		   				var recnt = participantsNumList.getSelectionModel().getSelection();
		   				participantsNumList.getSelectionModel().deselectAll();
		   				var recTotal = null;
		   				var cityTotal = 0;
		   				for(i in recs){
		   					if(recs[i].get("contrastObject") == "合计"){
		   						recTotal = recs[i];
		   					}else{
		   						cityTotal += Number(recs[i].get("contrastCountyValue"));
		   					}
		   				}
		   				if(recTotal){
		   					var contrastCityValueTotal = recTotal.get("contrastCityValue");
		   					recTotal.set("contrastCountyValue",cityTotal);
		   					recTotal.set("contrastCountValue",Number(cityTotal + Number(contrastCityValueTotal)));
		   				}
	   					if(me.totalNumber != cityTotal){
	   						$(Ext.get("contrastCountyValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "red";
	   					}else{
	   						$(Ext.get("contrastCountyValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "#000000";
	   					}
	   					if(me.contrastCountyValueTotal != Number(cityTotal + Number(recTotal.get("contrastCityValue")))){
	   						$(Ext.get("contrastCountValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "red";
	   					}else{
	   						$(Ext.get("contrastCountValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "#000000";
	   					}
	   					if(me.contrastCityValueTotal != cityTotal){
	   						$(Ext.get("contrastCityValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "red";
	   					}else{
	   						$(Ext.get("contrastCityValue_" + recnt[0].get("objectID")).dom).find("div")[0].style.color = "#000000";
	   					}
       				}
   				}else if(win.flag == 3){
       				rec.set("contrastCountValue",peopleNum);
       				var contrastCountValue = rec.get("contrastCountValue");
       				if(oldParticipantsNumList){
       					oldParticipantsNumList.getSelectionModel().deselectAll();
	       				oldParticipantsNumList.getSelectionModel().select(Number(me.myRowIndex));
	       				var rec_ = oldParticipantsNumList.getSelectionModel().getSelection()[0];
	       				oldParticipantsNumList.getSelectionModel().deselectAll();
	       				if(peopleNum != rec_.get("contrastCountValue")){
	       					$(Ext.get("contrastCountValue_" + me.myID).dom).find("div")[0].style.color = "red";
	       				}else{
	       					$(Ext.get("contrastCountValue_" + me.myID).dom).find("div")[0].style.color = "#000000";
	       				}
       				}else{
       					if(me.firstValue != peopleNum){
       						$(Ext.get("contrastCountValue_" + me.myID).dom).find("div")[0].style.color = "red";
       					}else {
       						$(Ext.get("contrastCountValue_" + me.myID).dom).find("div")[0].style.color = "#000000";
       					}
       				}
   				}
   				win.close();
            }
        }, {
            text: '关闭',
            scope: this,
            handler: function(button, event){
                var win = button.up('window');
                win.close();
            }
        }];
		this.callParent(arguments);
	}
});